package com.zbkj.admin.controller.merchant;

import com.zbkj.common.annotation.LogControllerAnnotation;
import com.zbkj.common.enums.MethodType;
import com.zbkj.common.model.water.RzDevice;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.water.RzDeviceSaveRequest;
import com.zbkj.common.request.water.RzDeviceSearchRequest;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.water.RzDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("api/admin/merchant/device")
@Api(tags = "商户设备管理控制器")
public class RzDeviceController {

    @Autowired
    private RzDeviceService rzDeviceService;

    @PreAuthorize("hasAuthority('merchant:device:list')")
    @ApiOperation(value = "设备分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<RzDevice>> getList(@Validated RzDeviceSearchRequest request) {
        return CommonResult.success(CommonPage.restPage(rzDeviceService.getPage(request)));
    }

    @PreAuthorize("hasAuthority('merchant:device:add')")
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.ADD, description = "新增设备")
    @ApiOperation(value = "新增设备")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<Object> save(@RequestBody RzDeviceSaveRequest request) {
        if (rzDeviceService.add(request)) {
            return CommonResult.success().setMessage("新增设备成功");
        }
        return CommonResult.failed().setMessage("新增设备失败");
    }

    @PreAuthorize("hasAuthority('merchant:device:update')")
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.UPDATE, description = "修改设备")
    @ApiOperation(value = "修改设备")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<Object> update(@RequestBody RzDeviceSaveRequest request) {
        if (rzDeviceService.updateDevice(request)) {
            return CommonResult.success().setMessage("修改设备成功");
        }
        return CommonResult.failed().setMessage("修改设备失败");
    }

    @PreAuthorize("hasAuthority('merchant:device:delete')")
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.DELETE, description = "删除设备")
    @ApiOperation(value = "删除设备")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult<Object> delete(@PathVariable(value = "id") Integer id) {
        if (rzDeviceService.deleteById(id)) {
            return CommonResult.success().setMessage("删除设备成功");
        }
        return CommonResult.failed().setMessage("删除设备失败");
    }

    @PreAuthorize("hasAuthority('merchant:device:update:online')")
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.UPDATE, description = "设备上下线")
    @ApiOperation(value = "设备上下线")
    @RequestMapping(value = "/update/online/{id}", method = RequestMethod.POST)
    public CommonResult<Object> updateOnline(@PathVariable("id") Integer id, @RequestParam("online") Boolean online) {
        if (rzDeviceService.updateOnline(id, online)) {
            return CommonResult.success().setMessage("设备状态更新成功");
        }
        return CommonResult.failed().setMessage("设备状态更新失败");
    }
}
